<?
include('./inc/master.php');

$newest_qa_id = -1;
$qas = '';

// Select the most recent QAs.
if ($result = $db->query("SELECT id, question, answer, aid FROM qa ORDER BY id DESC LIMIT 6")) {
  
  while ($row = $result->fetch_row()) {
    $newest_qa_id = $newest_qa_id == -1 ? $row[0] : $newest_qa_id;
    
    // Add the QA to the list.
    // TODO: Replace default-profile.jpg with the author's WP avatar.
    $qas .= '<li><img src="i/default-profile.jpg" width="78" height="92" alt="Profile Picture" /><p>Q: '. stripslashes($row[1]) .'</p><p>A: '. stripslashes($row[2]) .'</p></li>';
  }
  
  $result->close();
}
$qas = $qas == '' ? '<li></li>' : $qas;




//Seeing whos in the office now
$sql = "
  SELECT a.attended, a.name AS name1, users.name AS name2
  FROM (
      SELECT schedules.user_id_2, schedules.attended, users.name
      FROM schedules
      INNER JOIN users ON users.user_id = schedules.user_id_1
      WHERE start <= TIME_FORMAT( CURTIME( ) , '%H' )
      AND (end > TIME_FORMAT( CURTIME( ) , '%H' )
        OR end = 0)
      AND date = DATE_FORMAT( CURDATE( ) , '%Y-%m-%d' )
  ) AS a
  INNER JOIN users ON users.user_id = a.user_id_2";
$result = $db->query($sql);
$row = $result->fetch_object();

if($row->name1 || $row->name2)
  $whos_in_office = $row->name1 . " and " . $row->name2;

/* TODO once clocking in gets implemented, make "whos in office" dynamic.
$attended = $row->attended;
switch($attended) {
  case 0:
    break;
  case 1:
    $whos_in_office = $row->name1;
    break;
  case 2:      
    $whos_in_office = $row->name2;
    break;
  case 3:
    $whos_in_office = $row->name1 . " and " . $row->name2;
    break;
}
*/

//Get information about next shift
$next_shift;
$sql = "
  SELECT DISTINCT a.name AS name1, users.name AS name2, a.date, a.start, a.end
  FROM (
      SELECT schedules.user_id_2, schedules.start, schedules.end, schedules.date, schedules.open, schedules.attended, users.name
      FROM schedules
      INNER JOIN users ON users.user_id = schedules.user_id_1
      WHERE (date = CURDATE() 
      AND start > TIME_FORMAT( CURTIME( ) , '%H' ))
      OR date > CURDATE()
  ) AS a
  INNER JOIN users ON users.user_id = a.user_id_2
  ORDER BY a.date, a.start ASC
  LIMIT 0,1";
if($result = $db->query($sql) and $row = $result->fetch_object()) {
  $next_shift = $row->name1 . " and " . $row->name2;
  $then = mktime(0,0,0,substr($row->date,5,2),substr($row->date,8,2),substr($row->date,0,4));
  $next_shift .= "<br/>" . date('l',$then) ." " . $row->start . " - " . $row->end;
} 



//Get information about open shifts
$next_open;
$sql = "
  SELECT DISTINCT a.date, a.start, a.end
  FROM (
      SELECT schedules.user_id_2, schedules.start, schedules.end, schedules.date, schedules.open, schedules.attended, users.name
      FROM schedules
      INNER JOIN users ON users.user_id = schedules.user_id_1
      WHERE date >= CURDATE() 
      AND open > 0
  ) AS a
  INNER JOIN users ON users.user_id = a.user_id_2 ORDER BY a.date, a.start ASC LIMIT 0,10";

if($result = $db->query($sql)) {
  while($row = $result->fetch_object()) {    
    $shift_date = mktime(0,0,0,substr($row->date,5,2),substr($row->date,8,2),substr($row->date,0,4));
    $shift_start = mktime($row->start);
    $shift_end = mktime($row->end);
    $next_open[] =  date('l',$shift_date) . " " . date('g',$shift_start) . " - " . date('g',$shift_end);
  }
}






// Populate Templates
$smarty->assign('title','Dashboard');
$smarty->assign('whos_in_office',$whos_in_office);
$smarty->assign('next_shift',$next_shift);
$smarty->assign('next_open',$next_open);
$smarty->assign('dashboard', 1);
$smarty->assign('newest_qa_id', $newest_qa_id);
$smarty->assign('qas', $qas);
$smarty->display('index.tpl');
?>
